import Notify from '@vant/weapp/notify/notify';

import {
    meetingRoomPayOrderRq,
    selectReservationByIdRq,
} from "../../../../api/meeting/meetingRoom.js"

import {
    wxPaySignRq,
    wxMerchantOrderQueryRq
} from "../../../../api/pay/pay.js"

Page({

    /**
     * 页面的初始数据
     */
    data: {
        countdownTime: 0, // 单位毫秒
        timeData: {},
        showPay: false,
        detail: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let _this = this;
        _this.setData({
            ...options
        })
        // 获取详细信息
        _this.getDetail();
    },

    // 获取详细信息
    getDetail() {
        let _this = this;
        selectReservationByIdRq(_this.data.id).then(res => {
            let detail = res.data;
            // 支付时间 = 订单创建时间 + 5分钟
            let payTime = new Date(detail.createTime).getTime() + 5 * 60 * 1000
            // 当前时间
            let nowTime = new Date().getTime();
            // 还剩多长时间
            let residueTime = payTime - nowTime;
            // 订单时间过期
            if (residueTime < 0) {
                residueTime = 1
            }
            _this.setData({
                detail,
                countdownTime: residueTime, // 单位毫秒
            })
        })
    },

    // 时间改变
    changeTime(e) {
        let _this = this;
        this.setData({
            timeData: e.detail,
        });
        // 倒计时
        let hours = e.detail.hours
        let minutes = e.detail.minutes
        let seconds = e.detail.seconds
        // 倒计时结束
        if (hours <= 0 && minutes <= 0 && seconds <= 0) {
            // 消息提示
            Notify({
                message: '订单已过期!',
                color: '#FB4B4B',
                background: '#FBE9E9',
                duration: 5000,
            });
            _this.setData({
                showPay: false,
            });
        } else {
            _this.setData({
                showPay: true,
            });
        }
    },

    // 确认支付
    payComplete() {
        let _this = this;
        wxPaySignRq({
            prepayId: _this.data.detail.prepayId
        }).then(res => {
            console.log('wxPaySignRq', res);
            // 唤起支付
            _this.callPay(res.data)
        })
    },

    // 唤起支付
    callPay(data) {
        let _this = this;
        wx.requestPayment({
            timeStamp: data.timeStamp,
            nonceStr: data.nonceStr,
            package: data.prepayId,
            signType: data.signType,
            paySign: data.paySign,
            success(res) {
                console.log('success', res);

            },
            fail(res) {
                console.log('fail', res);

            },
            complete(res) {
                console.log('complete', res);
                // 查询订单详情
                _this.wxMerchantOrderQuery()
            }
        })
    },

    // 查询订单详情
    wxMerchantOrderQuery() {
        let _this = this
        wxMerchantOrderQueryRq({
            outTradeNo: _this.data.detail.oderNumber
        }).then(res => {
            console.log('wxMerchantOrderQuery', res);
            if (res.code == '200') {
                Notify({
                    type: 'primary',
                    message: res.msg
                });
                wx.reLaunch({
                  url: '/pages/meeting/pay/waitComplete/waitComplete',
                })
            } else {
                Notify({
                    type: 'warning',
                    message: res.msg
                });
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})